package com.szewec.ps;

import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.szewec.ps.base.Constants;
import com.szewec.ps.model.response.ResponseData;
import com.szewec.ps.myview.toast.bamtoast.BamToast;
import com.szewec.ps.ui.login.LoginActivity;
import com.szewec.ps.ui.login.SelectProjectAndStageActivity;
import com.szewec.ps.ui.tabbar.HomePageActivity;
import com.szewec.ps.util.CommonUtils;
import com.szewec.ps.util.LogUtil;
import com.szewec.ps.util.PreferincesContext;
import com.szewec.ps.util.SharedPreferencesUtil;
import com.szewec.ps.util.StringUtils;
import com.szewec.ps.util.fir.AppVersionInterface;
import com.szewec.ps.util.okhttp.OkHttp;

import java.util.HashMap;


public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    public static AppVersionInterface appVersionInterface;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String projectId = (String) SharedPreferencesUtil.getData(PreferincesContext.PROJECT_ID, "");

        appVersionInterface = new AppVersionInterface() {
            @Override
            public void needUpdate(String version) {

            }

            @Override
            public void newestVersion() {
                queryProjectList(projectId);
            }
        };

        /*
        判断网络状态:
            有网:a.本地存储有项目ID,更新项目信息,判断是否失效,失效则去重新选择项目和阶段
                b.如果能请求到项目信息,说明登陆过
            没网时:a.本地存储有项目ID,直接跳到首页
                   b.本地没存储项目ID,提示打开网络,否则退出应用
         */
        if (CommonUtils.isNetworkAvailable(this)) {
            //测试环境注释此行
//            AppVersionUpdate.getVersionInfoFromServer(this, "http://api.bq04.com/apps/latest/" + BaseUrl.appId + "?api_token="+BaseUrl.api_token);
            //生产环境注释此行
            queryProjectList(projectId);
        } else {
            BamToast.showText(this, "当前无网络,请打开网络后重试");
            finish();
        }
    }

    /**
     * 查询项目列表和阶段
     */
    private void queryProjectList(String projectId) {
        //更新项目和阶段
        HashMap<String, Object> param = new HashMap<>();
        param.put("pageIndex", 1);
        param.put("pageSize", 999);
        OkHttp.get("/tpms-plan-service/api/project/select", param, null, result -> {
//            result = result + "}";
            LogUtil.i(TAG, "最新的项目和阶段信息:" + result);
            ResponseData responseData = JSON.parseObject(result, ResponseData.class);
            if (responseData != null) {
                JSONObject res = JSON.parseObject(result);
                if (res != null && res.get("data") != null) {
                    String listString = res.getJSONArray("data").toString();
                    SharedPreferencesUtil.putData(PreferincesContext.PROJECT_AND_STAGE_INFO, listString);
                    if (StringUtils.isEmpty(projectId) //项目ID为空
                            || (!listString.contains(projectId))//最新项目消息不包含(本地信息失效)
                    ) {
                        toSelectProject();
                    } else {
                        toIndex();
                    }
                } else {
                    BamToast.showText(MainActivity.this, Constants.ErrorMsg.INTERNAL_SERVICE_ERROR, false);
                }
            }
        });
    }

    /**
     * 去首页
     */
    private void toIndex() {
        Intent intent = new Intent(MainActivity.this, HomePageActivity.class);
        startActivity(intent);
        finish();
    }

    private void toLogin() {
        Intent intent = new Intent(MainActivity.this, LoginActivity.class);
        startActivity(intent);
        finish();
    }

    /**
     * 去选项目
     */
    private void toSelectProject() {
        Intent intent = new Intent(MainActivity.this, SelectProjectAndStageActivity.class);
        startActivity(intent);
        finish();
    }
}
